package com.zmn.oms.api.controller.third.yunding;

import com.alibaba.fastjson.JSON;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.common.utils.RequestUtil;
import com.zmn.oms.third.yunding.YundingConsts;
import com.zmn.oms.third.yunding.dto.YundingCancelOrderDTO;
import com.zmn.oms.third.yunding.dto.YundingCreateOrderDTO;
import com.zmn.oms.third.yunding.dto.YundingCreateOrderResponseDTO;
import com.zmn.oms.third.yunding.dto.YundingResponseDTO;
import com.zmn.oms.third.yunding.service.YundingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 类描述：云丁鹿客对接接口
 *  用于接受云丁鹿客渠道创建和取消订单
 *
 * @author XieWenBing
 * @date 2020/09/08 15:00
 */

@RestController
@RequestMapping("/yunding")
@Slf4j
public class YundingController {

    @Autowired
    protected YundingService yundingService;

    @PostMapping("/api/createOrder")
    public YundingCreateOrderResponseDTO syncCreateOrder(HttpServletRequest request) {

        String bodyContent = RequestUtil.getPostParameter(request);
        log.info("【{}】 收到创建订单请求--【{}】",YundingConsts.CHANNEL_NAME,bodyContent);

        YundingCreateOrderResponseDTO responseDTO = new YundingCreateOrderResponseDTO();

        try {
            YundingCreateOrderDTO requestDTO = JSON.parseObject(bodyContent,YundingCreateOrderDTO.class);
            responseDTO.setOrderNumber(yundingService.createOrder(requestDTO));
            responseDTO.setCode(YundingConsts.RESPONSE_CODE_SUCCESS);
            responseDTO.setMessage(YundingConsts.RESPONSE_MESSAGE_SUCCESS);
        } catch (OmsBaseException omsBaseException) {
            responseDTO.setCode(omsBaseException.getCode().toString());
            responseDTO.setMessage(omsBaseException.getMessage());

            log.info("【{}】 创建订单异常，应答:【{}】，异常:【{}】",YundingConsts.CHANNEL_NAME,responseDTO.toString(),omsBaseException.toString());
        } catch (Exception exception) {
            responseDTO.setCode(YundingConsts.RESPONSE_CODE_SYSTEM_ERROR);
            responseDTO.setMessage("系统错误，创建订单失败");

            log.info("【{}】 创建订单异常，应答:【{}】，异常:【{}】",YundingConsts.CHANNEL_NAME,responseDTO.toString(),exception.toString());
            log.error(exception.getMessage(), exception);
        }

        log.info("【{}】 创建订单应答--【{}】",YundingConsts.CHANNEL_NAME,responseDTO.toString());
        return responseDTO;
    }

    @PostMapping("/api/cancelOrder")
    public YundingResponseDTO syncCancelOrder(HttpServletRequest request) {

        String bodyContent = RequestUtil.getPostParameter(request);
        log.info("【{}】 收到取消订单请求--【{}】",YundingConsts.CHANNEL_NAME,bodyContent);
        YundingResponseDTO responseDTO = new YundingResponseDTO();

        try {
            YundingCancelOrderDTO requestDTO = JSON.parseObject(bodyContent,YundingCancelOrderDTO.class);
            yundingService.cancelOrder(requestDTO);
            responseDTO.setCode(YundingConsts.RESPONSE_CODE_SUCCESS);
            responseDTO.setMessage(YundingConsts.RESPONSE_MESSAGE_SUCCESS);
        } catch (OmsBaseException omsBaseException) {
            responseDTO.setCode(omsBaseException.getCode().toString());
            responseDTO.setMessage(omsBaseException.getMessage());

            log.info("【{}】 创建订单异常，应答:【{}】，异常:【{}】",YundingConsts.CHANNEL_NAME,responseDTO.toString(),omsBaseException.toString());
        } catch (Exception exception) {
            responseDTO.setCode(YundingConsts.RESPONSE_CODE_SYSTEM_ERROR);
            responseDTO.setMessage("系统错误，取消失败");

            log.info("【{}】 取消订单异常，应答:【{}】，异常:【{}】",YundingConsts.CHANNEL_NAME,responseDTO.toString(),exception.toString());
        }

        log.info("【{}】 取消订单应答--【{}】",YundingConsts.CHANNEL_NAME,responseDTO.toString());
        return responseDTO;
    }
}
